//
//  AIHomeIconsFourView.m
//  aixianfeng
//
//  Created by wangwei on 2016/12/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIHomeIconsFourView.h"
#import "AIHomeIconsModel.h"
#import "AIHomeIconsController.h"

#import "AIHomeIconSButton.h"

#define ButtonCount  4

@interface AIHomeIconsFourView()

@property (nonatomic ,strong)NSMutableArray *btnArray;

@end

@implementation AIHomeIconsFourView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.btnArray = [NSMutableArray array];
        [self setupUI];
    }
    return self;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.btnArray = [NSMutableArray array];
        [self setupUI];
       
        
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor blueColor];
    //这里我们需要添加Button
    CGFloat btnX = 0,btnY = 0;
    CGFloat btnW = self.bounds.size.width * 0.25;
    CGFloat btnH = self.bounds.size.height;
    for (int i = 0; i < 4; i++) {
        UIButton *btn = [AIHomeIconSButton buttonWithType:0];
        btn.tag = i + 1;
        btnX = i * btnW;
        btn.frame = CGRectMake(btnX , btnY, btnW, btnH);
        
        btn.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0  alpha:1];
        [btn sd_setImageWithURL:[NSURL URLWithString:self.iconsModelArray[i].img] forState:UIControlStateNormal];
        [btn setTitle:self.iconsModelArray[i].name forState:UIControlStateNormal];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:btn];
        [self.btnArray addObject:btn];
        [btn addTarget:self action:@selector(actionBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
   // [self setupUI];
    

}
- (void)actionBtn:(UIButton *)sender {
    //点击btn 我们需要跳转控制器,这个我么通过响应者链条来做
    NSLog(@"%zd",sender.tag);
    
    //创建控制器跳转控制器
    AIHomeIconsController *iconsCV = [AIHomeIconsController new];
    iconsCV.customURL = self.iconsModelArray[sender.tag - 1].customURL;
    //iconsCV.title = self.iconsModelArray[self.tag - 1].name;
    [[self findNavController] pushViewController:iconsCV animated:YES];
}

- (void)setIconsModelArray:(NSArray<AIHomeIconsModel *> *)iconsModelArray {
    _iconsModelArray = iconsModelArray;
    [self setupUI];
}

@end
